/**
 * 基于easyUI的验证扩展
 * @author 晓铲子
 * @date 2012-8-7
 */
var remoteResult = {};//用来防止重复验证
var valitid = null;//远程验证时，等待用户输入完毕后再验证
(function($){ 
	$.extend($.fn.validatebox.defaults.rules, {
		loginName: {
			validator: function (value, param) {
				if (value.length < param[0] || value.length > param[1]) {
					$.fn.validatebox.defaults.rules.loginName.message = '用户名要在' + param[0] + '和' + param[1] + '之间！';
					return false;
				} else {
					if (!/^[\w]+$/.test(value)) {
						$.fn.validatebox.defaults.rules.loginName.message = '用户名只能英文字母、数字及下划线的组合！';
						return false;
					} else {
						var postdata = {};
						if (param[4]) {
							postdata[param[3]] = param[4];
						} else {
							postdata[param[3]] = value;
						}
						var result = null;
						var checkRe = remoteResult['loginName'];
						if(checkRe == undefined){
							checkRe = {account:'',isTrue:false};
						}
						if(checkRe['account'] == value){
							return checkRe['isTrue'];
						}
						if(valitid != null){
							clearTimeout(valitid);
							valitid = null;
						}
						valitid = setTimeout(function(){
							result = $.ajax({
								url: param[2],
								data: postdata,
								type: 'post',
								dataType: 'json',
								async: false,
								cache: false
							}).responseText;
							checkRe['account'] = value;
							if(typeof(result) == "string"){
								result = eval("("+ result +")");
							}
							if (result.messageObj == 'true') {
								$.fn.validatebox.defaults.rules.loginName.message = '用户名已存在！';
								checkRe['isTrue'] = false;
							} else if(result.messageObj == 'false'){
								checkRe['isTrue'] = true;
							}else{
								$.fn.validatebox.defaults.rules.loginName.message = "验证出现系统错误："+result.messageObj;
								checkRe['isTrue'] = false;
							}
							remoteResult['loginName'] = checkRe;
							return checkRe['isTrue'];
						}, 500);
						$.fn.validatebox.defaults.rules.loginName.message = '正在验证……';
						return checkRe['isTrue'];
					}
				}
			},
			message: '正在验证……'
		},
		checkPassword:{
			validator: function (value, param) {
				var pwd = $(param[0]).val();
				if(value != pwd){
					$.fn.validatebox.defaults.rules.checkPassword.message = '两次密码输入不一致！';
					return false;
				}else{
					return true;
				}
			}
		},
		checkIsExist:{
			validator: function (value,param){
				if (value.length < param[0] || value.length > param[1]) {
					$.fn.validatebox.defaults.rules.checkIsExist.message = param[4]+'要在' + param[0] + '和' + param[1] + '之间！';
					return false;
				} else {
					var postdata = {};
					var $obj = $(param[3]);
					postdata[$obj.attr("name")] = value;
					if($obj.attr("oldvalue")!= undefined && $obj.attr("oldvalue") == value){
						return true;
					}
					var checkRe = remoteResult['remoteName'];
					if(checkRe == undefined){
						checkRe = {cname:'',isTrue:false};
					}
					if(checkRe['cname'] == value){
						return checkRe['isTrue'];
					}
					var result = $.ajax({
						url: param[2],
						data: postdata,
						type: 'post',
						dataType: 'json',
						async: false,
						cache: false
					}).responseText;
					checkRe['cname'] = value;
					var aa = remoteResult;
					if(typeof(result) == "string"){
						result = eval("("+ result +")");
					}
					if (result.messageObj == 'true') {
						$.fn.validatebox.defaults.rules.checkIsExist.message = param[4]+'在系统已存在！';
						checkRe['isTrue'] = false;
					} else if(result.messageObj == 'false'){
						checkRe['isTrue'] = true;
					}else{
						$.fn.validatebox.defaults.rules.checkIsExist.message = "验证出现系统错误："+result.messageObj;
						checkRe['isTrue'] = false;
					}
					remoteResult['remoteName'] = checkRe;
					return checkRe['isTrue'];
				}
			}
		},
		setValiValue:{
			validator: function (value, param) {
				$.fn.validatebox.defaults.rules.setValiValue.message = param[1];
				return param[0];
			}
		},
		notValue:{
			validator: function (value, param) {
				var vals = param[0] && typeof (param[0]) == "string" ? [param[0]] : param[0];
				for(var x in vals){
					if(vals[x] == value){
						$.fn.validatebox.defaults.rules.notValue.message = param[1];
						return false;
					}
				}
				return true;
			}
		},
		checkMobile: {
			validator: function (value, param) {
				//电话号码检查
				if(/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|2|3|5|6|7|8|9])\d{8}$/.test(value)){
					return true;
				}else{
					$.fn.validatebox.defaults.rules.checkMobile.message = "电话号码不合法";
					return false;
				}
			}
		},
		checkChLength: {//中文占两个字符
			validator: function (value, param) {
				var valLength = 0;
				for(var i = 0;i<value.length;i++){
					var a = value.charAt(i);
					valLength++;
					if(escape(a).length > 4)valLength++;//中文字符的长度经编码之后大于4
				}
				if(param.length == 1){//只有最长
					if(valLength > param[0]){
						$.fn.validatebox.defaults.rules.checkChLength.message = '长度要在' + param[0] + '之内(中文占两个字符)！';
						return false;
					}
				}else{
					if(param.length >= 3){
						$(param[2]).text(valLength);
					}
					if (valLength < param[0] || valLength > param[1]) {
						$.fn.validatebox.defaults.rules.checkChLength.message = '长度要在' + param[0] + '和' + param[1] + '之间！';
						return false;
					}
				}
				return true;
			}
		}
	});
})(jQuery);
